//
//  GTCategoryMainController.m
//  GiftTalk
//
//  Created by Wan on 16/4/7.
//  Copyright © 2016年 Wan. All rights reserved.
//

#import "GTCategoryMainController.h"
#import "GTRaidersController.h"
#import "GTCategoryViewController.h"
#import "GTSearchViewController.h"

@interface GTCategoryMainController ()
/** 攻略控制器 */
@property (weak, nonatomic) GTRaidersController *raidersVc;
/** 礼物类别控制器 */
@property (weak, nonatomic) GTCategoryViewController *giftVc;
@end

@implementation GTCategoryMainController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 初始化子控制器 & 设置导航栏
    [self setupChildControllers];
    [self setupNavigationBar];
}


- (void)setupChildControllers {
    GTRaidersController *raidersVc = [[GTRaidersController alloc] init];
    [self addChildViewController:raidersVc];
    self.raidersVc = raidersVc;
    
    GTCategoryViewController *giftVc = [[GTCategoryViewController alloc] init];
    [self addChildViewController:giftVc];
    self.giftVc = giftVc;
}

- (void)setupNavigationBar {
    UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:@[@"攻略", @"礼物"]];
    segment.tintColor = [UIColor whiteColor];
    segment.width += 100;
    
    // 设置文字属性
    NSMutableDictionary *attr = [NSMutableDictionary dictionary];
    attr[NSForegroundColorAttributeName] = GTThemeColor;
    [segment setTitleTextAttributes:attr forState:UIControlStateSelected];
    
    // 导航栏设置
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_navigation_search"] style:UIBarButtonItemStyleDone target:self action:@selector(search)];
    
    // 默认选中segment 第一项
    [segment addTarget:self action:@selector(segmentClick:) forControlEvents:UIControlEventValueChanged];
    self.navigationItem.titleView = segment;
    segment.selectedSegmentIndex = 0;
    [self segmentClick:segment];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_navigation_search"] style:UIBarButtonItemStyleDone target:self action:@selector(search)];
}

- (void)segmentClick:(UISegmentedControl *)segment {
    if (segment.selectedSegmentIndex == 0) {
        [self.view addSubview:self.raidersVc.view];
        self.raidersVc.view.frame = self.view.bounds;
    } else {
        [self.view addSubview:self.giftVc.view];
        self.giftVc.view.frame = self.view.bounds;
    }
}


- (void)search {
    GTSearchViewController *searchVc = [[GTSearchViewController alloc] init];
    [self.navigationController pushViewController:searchVc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
